package com.cgnpc.scp.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.net.Socket;
import java.util.zip.ZipInputStream;

public class IOUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(IOUtil.class);

    /**
     * 关闭流
     */
    public static void closeStream(InputStream inputStream) {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Exception e) {
                LOGGER.error("closeInputStream.error;", e);
            }
        }
    }

    /**
     * 关闭流
     */
    public static void closeStream(InputStream... inputStreams) {
        if (inputStreams != null && inputStreams.length > 0) {
            for (InputStream inputStream : inputStreams) {
                closeStream(inputStream);
            }
        }
    }

    /**
     * 关闭流
     */
    public static void closeStream(OutputStream outputStream) {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (Exception e) {
                LOGGER.error("closeOutputStream.error;", e);
            }
        }
    }

    /**
     * 关闭流
     */
    public static void closeStream(OutputStream... outputStreams) {
        if (outputStreams != null && outputStreams.length > 0) {
            for (OutputStream outputStream : outputStreams) {
                closeStream(outputStream);
            }
        }
    }

    /**
     * 关闭流
     */
    public static void closeReader(Reader reader) {
        if (reader != null) {
            try {
                reader.close();
            } catch (Exception e) {
                LOGGER.error("closeReader.error;", e);
            }
        }
    }

    /**
     * 关闭流
     */
    public static void closeReader(Reader... readers) {
        if (readers != null && readers.length > 0) {
            for (Reader reader : readers) {
                closeReader(reader);
            }
        }
    }

    /**
     * 关闭流
     */
    public static void closeWriter(Writer writer) {
        if (writer != null) {
            try {
                writer.close();
            } catch (Exception e) {
                LOGGER.error("closeWriter.error;", e);
            }
        }
    }

    /**
     * 关闭流
     */
    public static void closeWriter(Writer... writers) {
        if (writers != null && writers.length > 0) {
            for (Writer writer : writers) {
                closeWriter(writer);
            }
        }
    }

    /**
     * 删除本地文件
     */
    public static boolean deleteLocalFile(File file) {
        if (file != null && file.exists()) {
            try {
                return file.delete();
            } catch (Exception e) {
                LOGGER.error("deleteLocalFile.error;", e);
            }
        }

        return false;
    }

    public static void close(InputStream inputStream) {
        closeStream(inputStream);
    }

    public static void close(OutputStream outputStream) {
        closeStream(outputStream);
    }

    public static void close(Reader reader) {
        closeReader(reader);
    }

    public static void close(Writer writer) {
        closeWriter(writer);
    }

    public static void closeSocket(Socket socket) {
        if (socket != null) {
            try {
                socket.close();
            } catch (Exception e) {
                LOGGER.error("error", e);
            }
        }
    }

    public static void close(ZipInputStream zipInputStream) {
        if (zipInputStream != null) {
            try {
                zipInputStream.closeEntry();
            } catch (Exception e) {
                LOGGER.error("error", e);
            }

            closeStream(zipInputStream);
        }
    }

}
